<?php

namespace App\Http\Controllers\Client\Common;

use App\Http\Controllers\RootController as Controller;
use App\Services\Sms\CaptchaService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;

class UploadController extends Controller
{
    public function handle(Request $request)
    {
        if (!$request->hasFile('file')) return $this->failure('请先上传图片');

        $file = $request->file('file');

        // 验证文件
        $validatedData = $request->validate([
            'file' => 'required|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);

        // 你可以自定义文件保存的路径和名称
        $filePath = 'uploads/' . $file->getClientOriginalName();

        // 使用 Laravel 的 Storage Facade 保存文件
        $url = 'uploads/' . $file->getClientOriginalName();

        Storage::disk('api')->put($url, file_get_contents($file->getRealPath()));

        // 总是返回成功
        return $this->success(['url'=>env('APP_URL').'api/'.$url]);
    }
}
